/* 
 * SELECT *
 * FROM (SELECT XX.*, ROWNUM AS RN
 * 	FROM (SELECT id, userNo, username, password, email, mobileNo, telNo, state
 * 		FROM t_mgmt_user
 * 		) XX
 * 	WHERE ROWNUM <= 40
 * 	) XXJ
 * WHERE RN > 20
 * ------------------------------
 * SELECT * FROM (
 * 		SELECT row_.*, ROWNUM rownum_ FROM (
 *              SELECT id, userNo, username, password, email, mobileNo, telNo, state
 *              FROM t_mgmt_user 
 *		) row_
 *	)
 * WHERE rownum_ > 20 AND rownum_ <= 40
 */
package com.honeybees.framework.pagination.dialect.impl;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.honeybees.framework.pagination.dialect.Dialect;

/**
 * <dl>
 * <dt><b> Oracle分页查询SQL语句 </b></dt>
 * <p>
 * <dd>功能描述</dd>
 * </dl>
 * <p>
 * Copyright (C) All rights reserved.
 * </p>
 *
 * @author 李远明
 * @version 2017-04-08 21:04:11
 * @since 2015-10-23 23:11:42
 */
public class OracleDialect extends AbstractDialect {

    /**
     * {@inheritDoc}
     *
     * @version 2015-10-23 23:11:42 新建
     * @version 2015-12-14 11:38:08 {@link PagerUtils#limit(String, String, int, int)}
     */
    @Override
    public String getPageSql(String querySql, int offset, int limit) {
        querySql = querySql.trim();

        // 截去 FOR UPDATE，否则查询报错。 2015-04-16 18:02
        if (querySql.toUpperCase().trim().endsWith(" FOR UPDATE")) {
            querySql = querySql.substring(0, querySql.length() - 11);
        }

        String pageSql = PagerUtils.limit(querySql, JdbcConstants.ORACLE, offset, limit);
        return pageSql;
    }

    /**
     * {@inheritDoc}
     *
     * @author 李远明
     * @version 2016年7月20日 下午8:55:07
     */
    @Override
    public String getCountSql(String querySql) {
        String countSql = PagerUtils.count(querySql, JdbcConstants.ORACLE);
        return countSql;
    }

    public static void main(String[] args) {
        String querySql = "SELECT id,userNo,username,password,email,mobileNo,telNo,state FROM t_mgmt_user WHERE state=? FOR UPDATE";
        Dialect instance = new OracleDialect();
        String pageSql0 = instance.getPageSql(querySql, 0, 20);
        String pageSqlN = instance.getPageSql(querySql, 20, 20);
        System.out.println("--limit0: \n" + pageSql0);
        System.out.println("--limitN: \n" + pageSqlN);
    }

}
